package com.knife.file.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knife.common.exception.CommonException;
import com.knife.file.app.KnifeStorageConfigService;
import com.knife.file.domain.entity.KnifeStorageConfig;
import com.knife.file.domain.service.factory.KnifeStoreFactory;
import com.knife.file.infra.cache.KnifeStorageConfigCache;
import com.knife.file.infra.mapper.KnifeStorageConfigMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author lth
 * @created 2025年09月10日 星期三 14:20
 */
@Service
public class KnifeStorageConfigServiceImpl implements KnifeStorageConfigService {
    @Resource
    private KnifeStorageConfigCache knifeStorageConfigCache;

    @Resource
    private KnifeStorageConfigMapper knifeStorageConfigMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeStorageConfig saveOrUpdateConfig(KnifeStorageConfig knifeStorageConfig) {
        Long id = knifeStorageConfig.getId();
        KnifeStorageConfig db;
        if (Objects.isNull(id)) {
            db = new KnifeStorageConfig();
        } else {
            db = knifeStorageConfigMapper.selectById(id);
        }
        if (knifeStorageConfig.getDefaultFlag() == 1) {
            // 只能有一个默认
            LambdaQueryWrapper<KnifeStorageConfig> knifeStorageConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
            knifeStorageConfigLambdaQueryWrapper.eq(KnifeStorageConfig::getDefaultFlag, 1)
                    .ne(Objects.nonNull(id), KnifeStorageConfig::getId, id);
            Long count = knifeStorageConfigMapper.selectCount(knifeStorageConfigLambdaQueryWrapper);
            if (count > 0) {
                throw new CommonException("只能有一个默认存储配置");
            }
        }
        db.setStorageType(knifeStorageConfig.getStorageType());
        db.setDomain(knifeStorageConfig.getDomain());
        db.setStorageCode(knifeStorageConfig.getStorageCode());
        db.setEndPoint(knifeStorageConfig.getEndPoint());
        db.setAccessKeyId(knifeStorageConfig.getAccessKeyId());
        // 密码加解密
        db.setAccessKeySecret(KnifeStoreFactory.encrypt(knifeStorageConfig.getAccessKeySecret()));
        db.setAppId(knifeStorageConfig.getAppId());
        db.setRegion(knifeStorageConfig.getRegion());
        db.setDefaultFlag(knifeStorageConfig.getDefaultFlag());
        db.setTenantId(knifeStorageConfig.getTenantId());
        db.setAccessControl(knifeStorageConfig.getAccessControl());
        db.setBucketPrefix(knifeStorageConfig.getBucketPrefix());
        db.setPrefixStrategy(knifeStorageConfig.getPrefixStrategy());
        db.setCreateBucketFlag(knifeStorageConfig.getCreateBucketFlag());
        db.setAttribute1(knifeStorageConfig.getAttribute1());
        db.setAttribute3(knifeStorageConfig.getAttribute3());
        db.setAttribute2(knifeStorageConfig.getAttribute2());
        db.setAttribute4(knifeStorageConfig.getAttribute4());
        db.setAttribute5(knifeStorageConfig.getAttribute5());
        db.setAttribute6(knifeStorageConfig.getAttribute6());
        db.setAttribute7(knifeStorageConfig.getAttribute7());
        db.setAttribute8(knifeStorageConfig.getAttribute8());
        db.setAttribute9(knifeStorageConfig.getAttribute9());
        db.setAttribute10(knifeStorageConfig.getAttribute10());
        db.setAttribute11(knifeStorageConfig.getAttribute11());
        db.setAttribute12(knifeStorageConfig.getAttribute12());
        db.setAttribute13(knifeStorageConfig.getAttribute13());
        db.setAttribute14(knifeStorageConfig.getAttribute14());
        db.setAttribute15(knifeStorageConfig.getAttribute15());
        if (Objects.isNull(id)) {
            knifeStorageConfigMapper.insert(db);
        } else {
            knifeStorageConfigMapper.updateById(db);
        }
        // 更新缓存
        knifeStorageConfigCache.setValue(db);
        return db;
    }

    @Override
    public Page<KnifeStorageConfig> selectKnifeStorageConfigPage(Page<KnifeStorageConfig> knifeStorageConfigPage, KnifeStorageConfig knifeStorageConfig) {
        LambdaQueryWrapper<KnifeStorageConfig> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .like(StringUtils.isNotEmpty(knifeStorageConfig.getStorageCode()), KnifeStorageConfig::getStorageCode, knifeStorageConfig.getStorageCode())
                .eq(KnifeStorageConfig::getTenantId, knifeStorageConfig.getTenantId())
                .orderByAsc(KnifeStorageConfig::getLastUpdateDate)
        ;
        Page<KnifeStorageConfig> page = knifeStorageConfigMapper.selectPage(knifeStorageConfigPage, knifeCodeLambdaQueryWrapper);
        for (KnifeStorageConfig record : page.getRecords()) {
            record.setAccessKeySecret(KnifeStoreFactory.decrypt(record.getAccessKeySecret()));
        }
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeStorageConfig deleteConfig(KnifeStorageConfig knifeStorageConfig) {
        KnifeStorageConfig db = knifeStorageConfigMapper.selectById(knifeStorageConfig.getId());
        if (db.getDefaultFlag() == 1) {
            throw new CommonException("默认配置不可删除");
        }
        knifeStorageConfigMapper.deleteById(db.getId());
        return db;
    }
}
